<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>callback</title>

    <script src="plugins/vue/dist/vue.js"></script>
    <script src="plugins/axios/dist/axios.js"></script>
    <script src="plugins/common.js"></script>
</head>
<body>
<div id="app">

</div>
<script type="text/javascript">
    new Vue({
        el : "#app",
        data : {
            code:''
        },
        methods : {
            wxLogin(){
                console.log(this.code)
                this.$http.post('/login/wechat/'+this.code).then(res=>res.data).then(res=>{
                    if(res.success){
                        console.log(res)
                        if(res.data.data.message == "false"){
                            // 没有绑定,要跳转到绑定页面
                            let {accessToken,openid} = res.data.data;
                            location.href = "/binder.html?accessToken="+accessToken+"&openid="+openid;
                        }else{
                            // 说明登录成功,将token和loginUser存入localStorage,跳转到首页
                            alert("登录成功");
                            // 将后端返回的token和loginUser存入localStorage
                            localStorage.setItem("token",res.data.data.token);
                            // 存信息到localStorage的时候,不能直接存对象,而要转成json的字符串才可以
                            localStorage.setItem("loginUser",JSON.stringify(res.data.data.loginUser));
                            // 跳转到首页
                            location.href = "/index.html";
                        }
                    }else{
                        alert(res.message);
                    }

                })
            }

        },
        mounted(){
            let url = location.href
            let obj = url2Param(url);   // {code:""}
            this.code = obj.code;
            this.wxLogin();
        }
    })
</script>


</body>
</html>